<?php
class Admin_Model_UsersMapper {

	protected $_dbTable;
	protected $db;

	public function setDbTable($dbTable) {
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable() {
		if (null === $this->_dbTable) {
			$this->setDbTable('Admin_Model_DbTable_Users');
		}
		
		return $this->_dbTable;
	}
	
	public function getDb() {
		if ($this->db === null) {
			$this->db = Zend_Registry::get('connectDB');
		}
		
		return $this->db;
	}

	public function countItem($arrParam) {
		$ssFilter = $arrParam['ssFilter'];
	
		$table = $this->getDbTable();
		$select = $table->select()->setIntegrityCheck(false);
		$select->from(array('usr' => 'tb_users'), 'COUNT(*) AS totalItem');
	
		$state = $ssFilter['state'];
		$keywords = $ssFilter['keywords'];
		$select->where($table->getAdapter()->quoteInto('usr.da_xoa=?', $state));
		$select->where( 'usr.email			LIKE ? OR ' .
						'usr.username		LIKE ? OR ' .
						'usr.ho_ten			LIKE ? OR ' .
						'usr.dia_chi		LIKE ? OR ' .
						'usr.dien_thoai		LIKE ?', '%' . $keywords . '%');
	
		$resultSet = $table->fetchAll($select);
		$array = $resultSet->toArray();
		return $array[0]['totalItem'];
	}
	
	public function fetch($arrParam) {
		$paginator = $arrParam['paginator'];
		$ssFilter = $arrParam['ssFilter'];
		$table = $this->getDbTable();
	
		$select = $table->select()->setIntegrityCheck(false);
		$select->from(array('usr' => 'tb_users'), '*');
		$select->joinLeft(array('adm2' => 'tb_users_admin'), 'usr.nguoi_cap_nhat=adm2.id', 'ho_ten AS ten_nguoi_cap_nhat');
	
		$state = $ssFilter['state'];
		$keywords = $ssFilter['keywords'];
		$col = $ssFilter['col'];
		$order = $ssFilter['order'];
		$select->where($table->getAdapter()->quoteInto('usr.da_xoa=?', $state));
		$select->where( 'usr.email			LIKE ? OR ' .
						'usr.username		LIKE ? OR ' .
						'usr.ho_ten			LIKE ? OR ' .
						'usr.dia_chi		LIKE ? OR ' .
						'usr.dien_thoai		LIKE ?', '%' . $keywords . '%');
	
		$select->order($col . ' ' . $order);
	
		$page = $paginator['currentPage'];
		$rowCount = $paginator['itemCountPerPage'];
		$start = (int) $rowCount * ($page - 1);
	
		$select->limit($rowCount, $start);
		return $this->processResultSet($table->fetchAll($select)->toArray(), 'view');
	}

	private function processResultSet($resultSet, $option) {
		$entries = array();
	
		if ($option == 'view') {
			foreach ($resultSet as $row) {
				$entry = new Admin_Model_TableColumn_Users($row);
				$entries[] = $entry;
			}
	
		} else {
			foreach ($resultSet as $row) {
				$entries[] = $row;
			}
		}
	
		return $entries;
	}
	
	public function fetchAll($query, $option) {
		if ($query === NULL) {
			$resultSet = $this->getDbTable()->fetchAll();
	
		} else {
			$table = $this->getDbTable();
			$select = $table->select();
			$select->from($table)->where($query);
			$resultSet = $this->getDbTable()->fetchAll($select);
		}
	
		return $this->processResultSet($resultSet->toArray(), $option);
	}

	// Update trang thai ẩn|hiện
	public function deleteItems($ids, $state, $currentUser) {
		try {
			$data = array(
					'da_xoa' 			=> $state == 1 ? true : false,
					'ngay_cap_nhat' 	=> date('Y-m-d H:i:s'),
					'nguoi_cap_nhat' 	=> $currentUser
			);
	
			if (is_array($ids)) {
				foreach ($ids as $id) {
					$data['id'] = trim($id);
					$this->getDbTable()->update($data, array('id = ?' => $id));
				}
	
			} else {
				$data['id'] = trim($ids);
				$this->getDbTable()->update($data, array('id = ?' => $ids));
			}
	
		} catch (Exception $e) {
			return false;
		}
	
		return true;
	}

	public function getLastId() {
		return $this->getDbTable()->getAdapter()->lastInsertId();
	}
	
}